﻿namespace Refract.Extensions
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    public static class FrameworkElementExtensions
    {
        #region Fields

        public static readonly DependencyProperty BoundingBoxProperty = 
            DependencyProperty.Register("BoundingBox", typeof(Rect), typeof(FrameworkElement), null);
        public static readonly DependencyProperty MaxZIndexProperty = 
           DependencyProperty.Register("MaxZIndex", typeof(int), typeof(Panel), null);

        #endregion Fields

        #region Methods

        public static Rect BoundingBox(this FrameworkElement element)
        {
            return GetBoundingBox(element);
        }

        public static Rect GetBoundingBox(FrameworkElement element)
        {
            Rect value = (Rect)element.GetValue(BoundingBoxProperty);
            if (value == default(Rect))
            {
                double top = (double) element.GetValue(Canvas.TopProperty);
                double left = (double) element.GetValue(Canvas.LeftProperty);

                Rect boundingBox = new Rect(left, top, element.ActualWidth, element.ActualHeight);

                SetBoundingBox(element, boundingBox);
                value = boundingBox;
            }

            return (Rect) value;
        }

        public static int GetMaxZIndex(Panel element)
        {
            int maxIndex = 0;

            object indexObject = element.GetValue(MaxZIndexProperty);
            if (indexObject == null)
            {
                foreach (var child in element.Children)
                {
                    int index = (int)Canvas.GetZIndex(child);
                    if (index > maxIndex) maxIndex = index;
                }
                SetMaxZIndex(element, maxIndex);
            }
            else maxIndex = (int) indexObject;

            return maxIndex;
        }

        public static int GetTopZIndex(this Panel element)
        {
            int topIndex = GetMaxZIndex(element);
            if(topIndex == Int32.MaxValue)
            {
            #warning Need to address this situation
            }
            topIndex++;
            SetMaxZIndex(element, topIndex);
            return topIndex;
        }

        public static int MaxZIndex(this Panel element)
        {
            return GetMaxZIndex(element);
        }

        public static void SetBoundingBox(FrameworkElement element, Rect value)
        {
            element.SetValue(BoundingBoxProperty, value);
        }

        public static void SetMaxZIndex(Panel element, int value)
        {
            element.SetValue(MaxZIndexProperty, value);
        }

        #endregion Methods
    }
}